import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class EmailSendUtil {
    public static boolean sendToMail(String to,String content) {
        try {
            Properties props = new Properties();
            props.put("username", PropertiesUtils.getProperties("sender.username"));
            props.put("password", PropertiesUtils.getProperties("sender.password"));
            props.put("mail.transport.protocol", PropertiesUtils.getProperties("mail.transport.protocol") );
            props.put("mail.smtp.host", PropertiesUtils.getProperties("mail.smtp.host"));
            props.put("mail.smtp.port", PropertiesUtils.getProperties("mail.smtp.port") );

            Session mailSession = Session.getDefaultInstance(props);

            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress(props.getProperty("username")));
            if (to == null || to.equals("")){
                msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            }
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(PropertiesUtils.getProperties("email.to")));
            msg.setSubject(PropertiesUtils.getProperties("email.subject"));
            if (content == null || content.equals("")){
                msg.setContent(PropertiesUtils.getProperties("email.content"),"text/html;charset=UTF-8");
            }
            msg.setContent(content,"text/html;charset=UTF-8");
            msg.saveChanges();
            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"), props
                    .getProperty("username"), props.getProperty("password"));
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    }
